package priv.happy.mealpickpro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import priv.happy.mealpickpro.domain.UserMeal;
import priv.happy.mealpickpro.dto.UserDTO;
import priv.happy.mealpickpro.mapper.UserMealMapper;
import priv.happy.mealpickpro.service.UserMealService;
import priv.happy.mealpickpro.util.ImgUtilv2;

/**
 * @author HAPPY
 * @description 针对表【user_meal】的数据库操作Service实现
 * @createDate 2023-07-02 11:41:39
 */
@Service
public class UserMealServiceImpl extends ServiceImpl<UserMealMapper, UserMeal>
        implements UserMealService {
    @Autowired
    UserMealMapper userMealMapper;

    @Override
    public UserMeal login(UserMeal user) {
        return userMealMapper.login(user);
    }

    @Override
    public int modifyUserInfo(UserDTO userInfo) {
        UserMeal userMeal = new UserMeal()
                .setId(userInfo.getId())
                .setName(userInfo.getName())
                .setGender(userInfo.getGender())
                .setBirthday(userInfo.getBirthday());
        if (userInfo.getImgName() != null) {
            // 校验上传的图像格式
            if (userInfo.getImgName() != null) {
                if (!"jpg".equalsIgnoreCase(userInfo.getImgName().split("\\.")[1])
                        && !"png".equalsIgnoreCase(userInfo.getImgName().split("\\.")[1])
                        && !"jpeg".equalsIgnoreCase(userInfo.getImgName().split("\\.")[1])) {

                    return 1;
                }
            }
            userMeal.setAvatar(ImgUtilv2.imageShowAvatarFolder + userInfo.getImgName());
            // 将图像写入本地文件
            ImgUtilv2.saveBase64ToImage(userInfo.getAvatarBase64(), ImgUtilv2.imageSaveAvatarFolder + userInfo.getImgName());

            return updateById(userMeal) ? 0 : 2;
        } else {
            return updateById(userMeal) ? 0 : 2;
        }
    }
}




